﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_list 的摘要说明
    /// </summary>
    public class qiugou_list : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        public void ProcessRequest(HttpContext context)
        {
            int quanzi_id = DTRequest.GetQueryInt("quanzi_id", 0);
            string keywords = DTRequest.GetString("keywords");
            int txtlong = DTRequest.GetInt("txtlong", 0);
            int txtwidth = DTRequest.GetInt("txtwidth", 0);
            int txtheight = DTRequest.GetInt("txtheight", 0);
            int classid = DTRequest.GetInt("classid", 0);

            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("id>0");
            if (!string.IsNullOrEmpty(keywords))
            {
                strWhere.Append(" and title like '%" + keywords + "%'");
            }
            if (txtlong > 0)
            {
                strWhere.Append(" and size_long=" + txtlong);
            }
            if (txtwidth > 0)
            {
                strWhere.Append(" and (size_wide=" + txtwidth + " or size_height=" + txtwidth + ")");
            }
            if (txtheight > 0)
            {
                strWhere.Append(" and (size_height=" + txtheight + " or size_wide=" + txtheight + ")");
            }
            if (quanzi_id > 0)
            {
                strWhere.Append(" and quanzi_id=" + quanzi_id);
            }

            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.qiugou bll = new BLL.qiugou();
            DataSet ds = bll.GetList(this.pageSize, this.page, strWhere.ToString(), "add_time desc", out this.totalCount);
            int total_page = this.totalCount / this.pageSize;

            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"qiugou\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr["size_long"] + "×" + dr["size_wide"] + "×" + dr["size_height"] + "\"");

                    string imgurl = dr["img_url"].ToString();
                    if (string.IsNullOrEmpty(imgurl))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        if (imgurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http:\\/\\/" + imgurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http:\\/\\/www.shicaiquanzi.com" + imgurl + "\"");
                        }
                    }
                    strTxt.Append(",\"content\":\"" + Microsoft.JScript.GlobalObject.escape(dr["content"].ToString().Replace("<p>", "").Replace("</p>", "")) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                    strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"qiugou\":[]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}